package edu.uta.cse5322_2011_team3;

import gui.ColorPane;

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;

public class UCController {

    private ArrayList<Phrase> phrases;
    private UseCaseGrid grid;
//    private ParserAbsraction abstractParser;

    public UCController() {
        grid = new UseCaseGrid();

    }

    /**
     * This method takes the input box values and colors the identified verb ,
     * nouns
     */
    public void performColor(ColorPane textArea) {

        boolean flag = false;
        String value = "";
        PhraseParserImpl p = Config.getPhraseParser();
        Document d = new Document(textArea.getText());

        this.phrases = p.identify(d);
        

        String srsText = textArea.getText();
        textArea.setText("");

        while (srsText.length() > 0) {
            for (Phrase ph : this.phrases) {
                value = ph.getContent();

                if (value.length() > srsText.length()) {
                    break;
                }

                if (0 == value.compareToIgnoreCase((srsText.substring(0, value.length())))) {
                    if (ph instanceof Verb) {
                        textArea.append(Color.red, value);
                    } else if (ph instanceof Noun) {
                        textArea.append(Color.blue, value);
                    }

                    srsText = srsText.substring(value.length());
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                textArea.append(Color.black, srsText.substring(0, 1));
                srsText = srsText.substring(1);
            } else {
                flag = false;
            }
        }
    }

    /**
     * This method takes identified verb, nouns and identifies the use case,
     * actor ,system
     * 
     */
    public UseCaseGrid performIdentify(ColorPane textArea) {
        UCParserImpl p = Config.getUCParser();
        
        Document d = new Document(textArea.getText());
        ArrayList<UseCase> usecases = p.identify(d);
        grid.setUseCases(usecases);
        System.out.println("UCController, perform identify "
                + grid.getUseCases());
        return grid;
    }

    /**
     * 
     * This method takes the useCases and shows it in table grid
     * 
     * @param useCase
     */
    public void showUsecases(DefaultTableModel model) {
        ArrayList<UseCase> usecases = grid.getUseCases();
        for (UseCase uc : usecases) {
            Object values[] = new Object[]{uc.getActor().getContent(),
                uc.getName(), uc.getSystem().getContent()};
            model.addRow(values);
        }

    }
}
